package com.emapp.lccode.activity

import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import android.os.Bundle
import android.widget.DatePicker
import androidx.lifecycle.Observer
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ProgressAdapter
import com.emapp.lccode.adapter.ProjectProgressAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.bean.Project
import com.emapp.lccode.databinding.ActivityProgressListBinding
import com.emapp.lccode.databinding.ActivityProjectProgressListBinding
import com.emapp.lccode.mode.ProjectDeviceViewModel
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.vertical
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.Toaster
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import me.hgj.jetpackmvvm.ext.parseState
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**项目进度
 */
class ProjectProgressListActivity : BaseActivity1<ProjectDeviceViewModel, ActivityProjectProgressListBinding>(),
    OnDateSetListener {
    var datas: ArrayList<Project> = arrayListOf()
    private val progressAdapter: ProjectProgressAdapter by lazy { ProjectProgressAdapter() }
    var page = 1
    var project_id=""
    var prject_name=""
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        project_id= intent.getStringExtra("project_id").toString()
        prject_name= intent.getStringExtra("project_name").toString()
        mViewBind.lvTitle.tvTitle.text = prject_name
        mViewBind.lvTitle.tvRight.text = ""
        EventBus.getDefault().register(this)
        mViewBind.rvList.run {
            vertical()
            adapter = progressAdapter
        }
        progressAdapter.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
            }
            isEmptyViewEnable = true
            emptyView = layoutInflater.inflate(R.layout.listitem_empty, mViewBind.rvList, false)
        }
        mViewBind.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(this@ProjectProgressListActivity))
            setRefreshFooter(ClassicsFooter(this@ProjectProgressListActivity))
            setOnRefreshListener {
                page = 1
                getList()
            }
            setOnLoadMoreListener {
                page++
                getList()
            }
        }
        showLoading()
        getList()
    }

    private fun getList() {
        mViewModel.getProjectProgressList(project_id)
    }

    override fun createObserver() {
        mViewModel.run {
            projectProgress.observe(this@ProjectProgressListActivity, Observer { resultState ->
                parseState(resultState, {
                    if (page == 1) {
                        datas.clear()
                        if (it != null&&it.data!=null) {
                            datas.addAll(it.data)
                            if (it.data.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishRefresh()
                            }
                        } else {
                            mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                        }
                    } else {
                        if (it != null&&it.data!=null) {
                            datas.addAll(it.data)
                            if (it.data.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishLoadMore()
                            }
                        } else {
                            mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                        }
                    }
                   // datas.reverse()
                    progressAdapter.submitList(datas)
                    progressAdapter.notifyDataSetChanged()
                }, {
                    Toaster.show(it.errorMsg)
                    mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                })
            })
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.tv_right -> {
                }

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }
            EventBusConfig.REFRESH_PROJECT_DEVICE -> {
                page = 1
                getList()
            }
            else -> {}
        }
    }


    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
    }

}
